{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d881ac96",
   "metadata": {},
   "source": [
    "# 创建 NumPy ndarray 对象\n",
    "NumPy 用于处理数组。 NumPy 中的数组对象称为 ndarray。\n",
    "\n",
    "我们可以使用 array() 函数创建一个 NumPy ndarray 对象。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0fc48df8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    "\n",
    "arr = np.array([1, 2, 3, 4, 5])\n",
    "\n",
    "print(arr)\n",
    "\n",
    "print(type(arr))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ef72ff4",
   "metadata": {},
   "source": [
    "type(): 这个内置的 Python 函数告诉我们传递给它的对象的类型。像上面的代码一样，它表明 arr 是 numpy.ndarray 类型。\n",
    "\n",
    "要创建 ndarray，我们可以将列表、元组或任何类似数组的对象传递给 array() 方法，然后它将被转换为 ndarray："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2bba3838",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n"
     ]
    }
   ],
   "source": [
    "# 使用元组创建 NumPy 数组：\n",
    "arr = np.array((1, 2, 3, 4, 5))\n",
    "\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de3d7239",
   "metadata": {},
   "source": [
    "# 数组中的维\n",
    "数组中的维是数组深度（嵌套数组）的一个级别。\n",
    "\n",
    "嵌套数组：指的是将数组作为元素的数组。\n",
    "\n",
    "## 0-D 数组\n",
    "0-D 数组，或标量（Scalars），是数组中的元素。数组中的每个值都是一个 0-D 数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c3666800",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "61\n"
     ]
    }
   ],
   "source": [
    "# 用值 61 创建 0-D 数组：\n",
    "arr = np.array(61)\n",
    "\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6072e3a1",
   "metadata": {},
   "source": [
    "## 1-D 数组\n",
    "其元素为 0-D 数组的数组，称为一维或 1-D 数组。\n",
    "\n",
    "这是最常见和基础的数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "26f0399d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n"
     ]
    }
   ],
   "source": [
    "# 创建包含值 1、2、3、4、5、6 的 1-D 数组：\n",
    "arr = np.array([1, 2, 3, 4, 5, 6])\n",
    "\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81453d26",
   "metadata": {},
   "source": [
    "## 2-D 数组\n",
    "其元素为 1-D 数组的数组，称为 2-D 数组。\n",
    "\n",
    "它们通常用于表示矩阵或二阶张量。\n",
    "\n",
    "NumPy 有一个专门用于矩阵运算的完整子模块 numpy.mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "20cbe184",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "# 创建包含值 1、2、3 和 4、5、6 两个数组的 2-D 数组：\n",
    "arr = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afc0cc21",
   "metadata": {},
   "source": [
    "## 3-D 数组\n",
    "其元素为 2-D 数组的数组，称为 3-D 数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8a4c5941",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[1 2 3]\n",
      "  [4 5 6]]\n",
      "\n",
      " [[1 2 3]\n",
      "  [4 5 6]]]\n"
     ]
    }
   ],
   "source": [
    "# 用两个 2-D 数组创建一个 3-D 数组，这两个数组均包含值 1、2、3 和 4、5、6 的两个数组：\n",
    "arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])\n",
    "\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2250ccab",
   "metadata": {},
   "source": [
    "## 检查维数？\n",
    "NumPy 数组提供了 ndim 属性，该属性返回一个整数，该整数会告诉我们数组有多少维。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8524dadb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# 检查数组有多少维：\n",
    "a = np.array(42)\n",
    "b = np.array([1, 2, 3, 4, 5])\n",
    "c = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])\n",
    "\n",
    "print(a.ndim) \n",
    "print(b.ndim) \n",
    "print(c.ndim) \n",
    "print(d.ndim)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "156dc036",
   "metadata": {},
   "source": [
    "## 更高维的数组\n",
    "数组可以拥有任意数量的维。\n",
    "\n",
    "在创建数组时，可以使用 ndmin 参数定义维数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5fb2ca67",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[[[1 2 3 4]]]]]\n",
      "number of dimensions : 5\n"
     ]
    }
   ],
   "source": [
    "# 创建一个有 5 个维度的数组，并验证它拥有 5 个维度：\n",
    "arr = np.array([1, 2, 3, 4], ndmin=5)\n",
    "\n",
    "print(arr)\n",
    "print('number of dimensions :', arr.ndim)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4759fe9f",
   "metadata": {},
   "source": [
    "# 数组索引\n",
    "## 访问数组元素\n",
    "数组索引等同于访问数组元素。\n",
    "\n",
    "您可以通过引用其索引号来访问数组元素。\n",
    "\n",
    "NumPy 数组中的索引以 0 开头，这意味着第一个元素的索引为 0，第二个元素的索引为 1，以此类推。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c1d122fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([1, 2, 3, 4])\n",
    "# 获取第一个元素\n",
    "print(arr[0])\n",
    "# 获取第二个元素\n",
    "print(arr[1])\n",
    "# 获取第三和第四个元素并将其相加\n",
    "print(arr[2] + arr[3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1879c5a",
   "metadata": {},
   "source": [
    "## 访问 2-D 数组\n",
    "要访问二维数组中的元素，我们可以使用逗号分隔的整数表示元素的维数和索引。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "58f5a12d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4  5]\n",
      " [ 6  7  8  9 10]]\n",
      "2nd element on 1st dim:  2\n",
      "5th element on 2nd dim:  10\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])\n",
    "print(arr)\n",
    "# 访问第一维中的第二个元素\n",
    "print('2nd element on 1st dim: ', arr[0, 1])\n",
    "# 第二维中的第五个元素\n",
    "print('5th element on 2nd dim: ', arr[1, 4])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39ea329b",
   "metadata": {},
   "source": [
    "## 访问 3-D 数组\n",
    "要访问 3-D 数组中的元素，我们可以使用逗号分隔的整数来表示元素的维数和索引。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ec02fda2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 1  2  3]\n",
      "  [ 4  5  6]]\n",
      "\n",
      " [[ 7  8  9]\n",
      "  [10 11 12]]]\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "# 访问第一个数组的第二个数组的第三个元素：\n",
    "arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])\n",
    "print(arr)\n",
    "# 访问第一个数组的第二个数组的第三个元素\n",
    "print(arr[0, 1, 2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f969d4a9",
   "metadata": {},
   "source": [
    "## 负索引\n",
    "使用负索引从尾开始访问数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "cef53964",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4  5]\n",
      " [ 6  7  8  9 10]]\n",
      "Last element from 2nd dim:  10\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])\n",
    "print(arr)\n",
    "# 打印第二个维中的的最后一个元素\n",
    "print('Last element from 2nd dim: ', arr[1, -1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a66fa52c",
   "metadata": {},
   "source": [
    "# 数组裁切\n",
    "## 裁切数组\n",
    "python 中裁切的意思是将元素从一个给定的索引带到另一个给定的索引。\n",
    "\n",
    "我们像这样传递切片而不是索引：[start：end]。\n",
    "\n",
    "我们还可以定义步长，如下所示：[start：end：step]。\n",
    "\n",
    "如果我们不传递 start，则将其视为 0。\n",
    "\n",
    "如果我们不传递 end，则视为该维度内数组的长度。\n",
    "\n",
    "如果我们不传递 step，则视为 1。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "7b668881",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 3 4 5]\n",
      "[5 6 7]\n",
      "[1 2 3 4]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "arr = np.array([1, 2, 3, 4, 5, 6, 7])\n",
    "\n",
    "# 裁切索引 1 到索引 5 的元素\n",
    "print(arr[1:5])\n",
    "\n",
    "#裁切数组中索引 4 到结尾的元素\n",
    "print(arr[4:])\n",
    "\n",
    "#裁切从开头到索引 4（不包括）的元素\n",
    "print(arr[:4])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76a0edcd",
   "metadata": {},
   "source": [
    "## 负裁切\n",
    "使用减号运算符从末尾开始引用索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "e41d046b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 6]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([1, 2, 3, 4, 5, 6, 7])\n",
    "# 从末尾开始的索引 3 到末尾开始的索引 1，对数组进行裁切\n",
    "print(arr[-3:-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f303daa",
   "metadata": {},
   "source": [
    "## STEP\n",
    "使用 step 值确定裁切的步长："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "fb03cd92",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 4]\n",
      "[1 3 5 7]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([1, 2, 3, 4, 5, 6, 7])\n",
    "# 从索引 1 到索引 5，返回相隔的元素\n",
    "print(arr[1:5:2])\n",
    "\n",
    "#返回数组中相隔的元素\n",
    "print(arr[::2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31aef5fa",
   "metadata": {},
   "source": [
    "## 裁切 2-D 数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "d2421aaf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7 8 9]\n",
      "[3 8]\n",
      "[[2 3 4]\n",
      " [7 8 9]]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])\n",
    "\n",
    "# 从第二个元素开始，对从索引 1 到索引 4（不包括）的元素进行切片\n",
    "print(arr[1, 1:4])\n",
    "\n",
    "# 从两个元素中返回索引2\n",
    "print(arr[0:2, 2])\n",
    "\n",
    "# 从两个元素裁切索引 1 到索引 4（不包括），这将返回一个 2-D 数组\n",
    "print(arr[0:2, 1:4])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
